<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域 -->
    <el-card class="box-card">
      <!--搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input placeholder="请输入内容" v-model="homeForm.inputusername">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUseroneList"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="adddialogvisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="mailbox" label="邮箱" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="电话" align="center">
        </el-table-column>
        <el-table-column prop="role_mg" label="角色" align="center">
        </el-table-column>
        <el-table-column
          label="状态"
          prop="state_mg"
          align="center"
          :filters="[
            { text: '正常', value: '正常' },
            { text: '异常', value: '异常' }
          ]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
        >
        <template slot-scope="scope">
        <el-tag
          :type="scope.row.state_mg === '正常' ? 'success' : 'danger'"
          disable-transitions>{{scope.row.state_mg}}</el-tag>
      </template>
        </el-table-column>
        <el-table-column label="操作" width="180px" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.name)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserByid(scope.row.name)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
              @click="fenpeijuesemysql"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="[100, 200, 300, 400]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 对话框-->
    <el-dialog
      title="添加用户"
      :visible.sync="adddialogvisible"
      width="50%"
      @close="adddialogclose"
    >
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormrulesRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="mysqlname">
          <el-input v-model="addForm.mysqlname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="mysqlpassword">
          <el-input v-model="addForm.mysqlpassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mysqlmailbox">
          <el-input v-model="addForm.mysqlmailbox"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mysqlphone">
          <el-input v-model="addForm.mysqlphone"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogvisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改用户" :visible.sync="editdialogVisible" width="50%">
      <el-form
        ref="editFormRef"
        :rules="editFormRules"
        :model="editForm"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="editnamefrom">
          <el-input v-model="editForm.name" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="editmailboxfrom">
          <el-input v-model="editForm.mailbox"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="editphonefrom">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        currentPage: 1, // 当前页数
        pageSize: 8 // 每页获取条数（页面大小）
      },
      trueandfalse: 'true',
      tableData: [],
      total: 0,
      homeForm: {
        inputusername: ''
      },
      // 控制对话框显示
      adddialogvisible: false,
      editdialogVisible: false,
      // 修改用户查询到的信息
      editForm: {},
      addForm: {
        mysqlname: '',
        mysqlphone: '',
        mysqlmailbox: '',
        mysqlpassword: ''
      },
      addFormrules: {
        mysqlname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名长度在3~10个字符之间',
            trigger: 'blur'
          }
        ],
        mysqlpassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 5,
            max: 15,
            message: '密码长度在5~15个字符之间',
            trigger: 'blur'
          }
        ],
        mysqlmailbox: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        mysqlphone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            min: 11,
            max: 11,
            message: '手机号码长度为11数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUseroneList() {
      this.$axios
        .post('http://localhost:3000/api/Stu/showoneStu', {
          // 发送请求 跳转页面
          name: this.homeForm.inputusername
        })
        .then(res => {
          console.log(res)
          this.tableData = res.data
          this.total = res.data.length
        })
        .catch(res => {
          console.log('错误' + res)
        })
    },
    xiugaimysql() {},
    async getUserList() {
      this.$axios
        .get('http://localhost:3000/api/Stu/showStu')
        .then(response => {
          console.log(response)
          this.tableData = response.data
          this.total = response.data.length
        })

      const { data: res } = await this.$axios.get(
        'http://localhost:3000/api/Stu/showStu',
        {
          params: this.queryInfo
        }
      )
      console.log(res)
    },
    shuaxin() {},
    handleSizeChange(newSize) {
      console.log(newSize)
    },
    handleCurrentChange(newPage) {
      console.log(newPage)
    },
    userStateChanged(userinfo) {
      console.log(userinfo)
      // this.$axios.post('http://localhost:3000/api/Stu/addStu', (req, res) => {
      //   console.log(req.body)
      //   res.send(req.body)
      //  {"username":"1","pwd":"1"}
      //  })
      this.$axios
        .post('http://localhost:3000/api/Stu/updateStu', {
          // 发送请求 跳转页面
          mailbox: userinfo.mailbox,
          name: userinfo.name,
          role_mg: userinfo.role_mg,
          state_mg: userinfo.state_mg,
          phone: userinfo.phone,
          password: userinfo.password
        })
        .then(response => {
          console.log(response.data)
        })
        .catch(response => {
          console.log('错误' + response)
        })
    },
    adddialogclose() {
      this.$refs.addFormrulesRef.resetFields()
    },
    addUser() {
      this.$refs.addFormrulesRef.validate(async valid => {
        if (!valid) return
        await this.$axios
          .post('http://localhost:3000/api/Stu/addStu', {
            // 发送请求 跳转页面
            mailbox: this.addForm.mysqlmailbox,
            name: this.addForm.mysqlname,
            role_mg: '普通用户',
            state_mg: '正常',
            phone: this.addForm.mysqlphone,
            password: this.addForm.mysqlpassword
          })
          .then(response => {
            console.log(response)
            if (response.status !== 200) {
              this.$message.console.error('添加用户失败！')
            }
            this.$message.success('添加用户成功！')
            this.adddialogvisible = false
            this.getUserList()
          })
          .catch(response => {
            console.log('错误' + response)
          })
      })
    },
    showEditDialog(id) {
      // console.log(name)
      this.$axios
        .post('http://localhost:3000/api/Stu/showoneStu', {
          // 发送请求 跳转页面
          name: id
        })
        .then(res => {
          console.log(res)
          this.editForm = res.data[0]
          console.log(res.data[0])
        })
        .catch(res => {
          console.log('错误' + res)
        })

      this.editdialogVisible = true
    },
    editUser() {
      this.$axios
        .post('http://localhost:3000/api/Stu/updateStu', {
          // 发送请求 跳转页面
          mailbox: this.editForm.mailbox,
          name: this.editForm.name,
          role_mg: this.editForm.role_mg,
          state_mg: this.editForm.state_mg,
          phone: this.editForm.phone,
          password: this.editForm.password
        })
        .then(response => {
          console.log(response)
          this.$message.success('修改用户成功！')
          this.editdialogVisible = false
        })
        .catch(response => {
          console.log('错误' + response)
        })
    },
    async removeUserByid(id) {
      console.log(id)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      this.$axios
        .post('http://localhost:3000/api/Stu/delStu', {
          // 发送请求 跳转页面
          name: id
        })
        .then(res => {
          console.log(res)
          this.$message.success('删除用户成功！')
          this.getUserList()
        })
        .catch(res => {
          console.log('错误' + res)
        })
    }
  }
}
</script>
<style lang="less" scoped></style>
